﻿#define _CRT_SECURE_NO_WARNINGS 1

//1.天使果冻
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;

int n, t, x;
int a[N];
int dp[N];

int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];

	dp[2] = min(a[1], a[2]);
	int m = max(a[1], a[2]);
	for (int i = 3; i <= n; ++i)
	{
		if (a[i] >= m)
		{
			dp[i] = m;
			m = a[i];
		}
		else if (a[i] > dp[i - 1])
		{
			dp[i] = a[i];
		}
		else
		{
			dp[i] = dp[i - 1];
		}
	}

	cin >> t;
	while (t--)
	{
		cin >> x;
		cout << dp[x] << endl;
	}
	return 0;
}

// 2.dd爱旋转
#include <iostream>

using namespace std;

const int N = 1010;
int n;
int g[N][N];

void setRow() // ⾏对称
{
	for (int i = 0; i < n / 2; i++)
	{
		for (int j = 0; j < n; j++)
		{
			swap(g[i][j], g[n - 1 - i][j]);
		}
	}
}

void setCol() // 列对称
{
	for (int j = 0; j < n / 2; j++)
	{
		for (int i = 0; i < n; i++)
		{
			swap(g[i][j], g[i][n - 1 - j]);
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> g[i][j];
		}
	}

	int q, x;
	cin >> q;

	int row = 0, col = 0;
	while (q--)
	{
		cin >> x;
		if (x == 1) row++, col++;
		else row++;
	}

	row %= 2; col %= 2;
	if (row) setRow();
	if (col) setCol();
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << g[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}

// 3.小红取数
#include <iostream>
#define int long long
using namespace std;

int n, k;
int a[1010];
int dp[1010][1010];

signed main() {
	cin >> n >> k;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];

	for (int i = 1; i <= k; ++i)
		dp[0][i] = -1;

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 0; j < k; ++j)
		{
			dp[i][j] = dp[i - 1][j];
			if (dp[i - 1][(j - a[i] % k + k) % k] != -1)
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][(j - a[i] % k + k) % k] + a[i]);
		}
	}

	if (dp[n][0] <= 0)
		cout << -1 << endl;
	else
		cout << dp[n][0] << endl;
	return 0;
}